import mongoose from '../config/dbHelper';
import moment from 'moment';

const Schema = mongoose.Schema;
const SignRecord = new Schema({
  uid: { type: String, ref: 'users' },
  createTime: { type: Date },
  favs: { type: Number },
  lastSign: { type: Date }
});

SignRecord.pre('save', function(next) {
  this.createTime = moment().format('YYYY-MM-DD HH:mm:ss');
  next();
});

SignRecord.statics = {
  getById: function (uid) {
    // 查找该 uid 下时间最近的一次签到记录
    return this.findOne({ uid }).sort({ createTime: -1 })
  }
}

const signRecordModel = mongoose.model('signRecord', SignRecord);
export default signRecordModel;
